package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.CustomerExample;
import com.chixing.service.LoginCustomerService;
import com.chixing.util.MD5Utils;
import com.chixing.util.RSA.AESUtil;
import com.chixing.util.RSA.RSAUtil;
import com.chixing.util.ServerResponse;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.List;

@Service
public class LoginCustomerServiceImpl implements LoginCustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public ServerResponse Login(Long custTel, String custPassword) {
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andCustTelEqualTo(custTel);
        String newPassword = MD5Utils.formPassToDBPass(custPassword,MD5Utils.SALT);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        Customer customer1 = null;
        if(customerList.size()>0) {
            customer1 = customerList.get(0);
            String pwd = customer1.getCustPassword();
            if(pwd.equals(newPassword)){
                return ServerResponse.getSuccess(customer1);
            }else {
                return ServerResponse.getFail(null);
            }
        }
        return ServerResponse.getFail(null);
    }


//    @Override
//    public ServerResponse Login1(Long custTel, String custPassword) {
//        System.out.println("密文是:"+custPassword);
//        System.out.println("手机"+custTel);
//        CustomerExample customerExample = new CustomerExample();
//
//        String privateKey = getPrivateKey();
//
//        String encrypt = null;
//        try {
//            System.out.println("RSA解密中.....");
//            String decrypted = RSAUtil.privateDecrypt(custPassword, RSAUtil.getPrivateKey(privateKey));
//
//            System.out.println("RSA解密后：" + decrypted);
//
//
//            encrypt = AESUtil.encrypt(decrypted, key);
//
//            System.out.println("AES加密后：" + encrypt);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        try{
//            Long usertrl=Long.parseLong(String.valueOf(custTel));
//            customerExample.createCriteria().andCustTelEqualTo(usertrl).andCustPasswordEqualTo(encrypt);
//        }catch (NumberFormatException e){
//            customerExample.createCriteria().andCustEmailEqualTo(String.valueOf(custTel)).andCustPasswordEqualTo(encrypt);
//        }
//        List<Customer> customers=customerMapper.selectByExample(customerExample);
//
//        if(customers.size()>0){
//            return ServerResponse.getSuccess(customers.get(0));
//        }
//        return null;
//    }
}
//                注册的密码d3f2a61d18fbb68970a862d3ca79746d
//最终持久化到数据库中的密码是：cedbb232d58fc2495f119c28511e77ba